package com.example.poemtest.index.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.poemtest.R;
import com.example.poemtest.databinding.ItemPoetryRelayLeftBinding;
import com.example.poemtest.databinding.ItemPoetryRelayRightBinding;
import com.example.poemtest.entity.PoetryInfoBean;
import com.example.poemtest.index.entity.PoetryRelayBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 诗词接龙的适配器
 */
public class PoetryRelayAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final LayoutInflater mInflater;
    List<PoetryInfoBean.PoetryInfo> mLists;
    Context mContext;
    private String startText;

    public static final int LEFT_CHAT = 0x11;//左侧聊天
    public static final int RIGHT_CHAT = 0x12;//右侧聊天


    public PoetryRelayAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLists = new ArrayList<>();
    }

    public List<PoetryInfoBean.PoetryInfo> getLists() {
        return mLists;
    }

    public void addItem(PoetryInfoBean.PoetryInfo bean) {
        mLists.add(bean);
        notifyItemRangeInserted(mLists.size(), 1); //因为是xrecyclerview 不需要 -1
    }

    public void addLists(List<PoetryInfoBean.PoetryInfo> lists) {
        Collections.reverse(lists);
        mLists.addAll(0, lists);
        notifyItemRangeInserted(0, lists.size());
    }

    public void setLists(List<PoetryInfoBean.PoetryInfo> lists) {
        Collections.reverse(lists);
        mLists.addAll(lists);
        notifyDataSetChanged();
    }

    public void setList(List<PoetryInfoBean.PoetryInfo> lists) {
        mLists = lists;
        notifyDataSetChanged();
    }

    public void deleteLists(PoetryInfoBean.PoetryInfo msg) {
        if (null!=mLists&&mLists.size()>0){
            int position = 0;
//            for (int i = 0; i < mLists.size(); i++) {
//                if (mLists.get(i).getClientMsgID().equals(msg.getClientMsgID())){
//                    position = i;
//                }
//            }
            mLists.remove(position);
            notifyItemRemoved(position+1);  //因为是xrecyc
        }
    }

    public void setStartText(String startText) {
        this.startText = startText;
    }

    public interface OnItemClickListener {
        void OnLongClick(int position, View view);
    }

    OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case LEFT_CHAT: {
                return new LeftViewHolder(mInflater.inflate(R.layout.item_poetry_relay_left, parent, false));
            }
            default: {
                return new RightViewHolder(mInflater.inflate(R.layout.item_poetry_relay_right, parent, false));
            }
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        PoetryInfoBean.PoetryInfo entity = mLists.get(position);
        switch (getItemViewType(position)) {
            case LEFT_CHAT: {
                LeftViewHolder leftViewHolder = (LeftViewHolder) holder;
                leftViewHolder.binding.TvMsg.setText(entity.getPoetryContent());

            }
            break;
            case RIGHT_CHAT: {
                RightViewHolder rightViewHolder = (RightViewHolder) holder;
                rightViewHolder.binding.TvMsg.setText(entity.getPoetryContent());
            }
            break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mLists.get(position).isIsmyself()) {
            return RIGHT_CHAT;
        }else {
            return LEFT_CHAT;
        }
    }

    @Override
    public int getItemCount() {
        return mLists.size();
    }

    //左侧聊天框
    class LeftViewHolder extends RecyclerView.ViewHolder {
        ItemPoetryRelayLeftBinding binding;

        public LeftViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }
    }

    //右侧聊天框
    class RightViewHolder extends RecyclerView.ViewHolder {
        ItemPoetryRelayRightBinding binding;

        public RightViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }
    }
}
